﻿using System.Windows;
using Peter.Sdw.Logic;

namespace Peter.Sdw.View
{
   /// <summary>
   /// Interaction logic for EntryEditView.xaml
   /// </summary>
   public partial class EntryEditView
   {
      private EntryModel m_Entry;

      /// <summary>
      /// Initializes a new entry edit view.
      /// </summary>
      public EntryEditView ()
      {
         InitializeComponent ();
         this.DataContextChanged += this.OnDataContextChanged;
      }

      /// <summary>
      /// Occurs when the data context changes.
      /// </summary>
      /// <param name="sender">This.</param>
      /// <param name="e">DependencyPropertyChangedEventArgs</param>
      private void OnDataContextChanged (object sender, DependencyPropertyChangedEventArgs e)
      {
         this.DataContextChanged -= this.OnDataContextChanged;
         this.m_Entry = this.DataContext as EntryModel;
      }

      /// <summary>
      /// Occurs when the selection in the text box changes.
      /// </summary>
      /// <param name="sender">The text box.</param>
      /// <param name="e">RoutedEventArgs</param>
      private void OnTextSelectionChanged (object sender, RoutedEventArgs e)
      {
         if (this.m_Entry != null) {
            this.m_Entry.StartIndex = this.TextBox.SelectionStart;
            this.m_Entry.EndIndex = this.TextBox.SelectionStart + this.TextBox.SelectionLength;
         }
      }
   }
}
